package com.ruoyi.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ErpSupplierManagement;
import com.ruoyi.service.IErpSupplierManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商管理Controller
 * 
 * @author erp
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/erp_supplier/management")
public class ErpSupplierManagementController extends BaseController {
    @Autowired
    private IErpSupplierManagementService erpSupplierManagementService;

    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('erp_supplier:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpSupplierManagement erpSupplierManagement) {
        startPage();
        List<ErpSupplierManagement> list = erpSupplierManagementService.selectErpSupplierManagementList(erpSupplierManagement);
        return getDataTable(list);
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('erp_supplier:management:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpSupplierManagement erpSupplierManagement) {
        List<ErpSupplierManagement> list = erpSupplierManagementService.selectErpSupplierManagementList(erpSupplierManagement);
        ExcelUtil<ErpSupplierManagement> util = new ExcelUtil<ErpSupplierManagement>(ErpSupplierManagement.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp_supplier:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(erpSupplierManagementService.selectErpSupplierManagementById(id));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('erp_supplier:management:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpSupplierManagement erpSupplierManagement) {
        return toAjax(erpSupplierManagementService.insertErpSupplierManagement(erpSupplierManagement));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('erp_supplier:management:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpSupplierManagement erpSupplierManagement) {
        return toAjax(erpSupplierManagementService.updateErpSupplierManagement(erpSupplierManagement));
    }

    /**
     */
    @PreAuthorize("@ss.hasPermi('erp_supplier:management:remove')")
    @Log( businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(erpSupplierManagementService.deleteErpSupplierManagementByIds(ids));
    }

    @PostMapping("/updateaudit")
    public ResponseEntity<?> updateaudit(@RequestParam("id") int id , @RequestParam("opinion") String  opinion, @RequestParam("choice") int choice) {
        ErpSupplierManagement erpSupplierManagement = new ErpSupplierManagement();
        erpSupplierManagement.setOpinion(opinion);
        erpSupplierManagement.setChoice(choice);
        erpSupplierManagement.setId(id);
        Map<String, Integer> i = erpSupplierManagementService.updateAudit( erpSupplierManagement);


        //添加审批记录

        if (i.get("提交成功？") > 0) {
            return ResponseEntity.ok(AjaxResult.success("审核成功"));
        } else {
            return ResponseEntity.status(500).body(AjaxResult.error("审核失败"));
        }
    }
    /*审核供应商管理 保存*/
    @PostMapping("/saveUpdateaudit")
    public AjaxResult saveAudit(@RequestBody ErpSupplierManagement erpSupplierManagementrpSupplierManagement) {
        System.out.println(erpSupplierManagementrpSupplierManagement);
        if (StringUtils.isBlank(erpSupplierManagementrpSupplierManagement.getTransactionCurrency())) {
            System.out.println("发送过来对象为空");
        }
            System.out.println("开始保存");
            boolean save = erpSupplierManagementService.save(erpSupplierManagementrpSupplierManagement);
            if (save) {
                return AjaxResult.success("保存成功");
            }
            return AjaxResult.error("保存失败");
    }

        /*审核供应商管理 审核驳回*/
        @PostMapping("/reject/{id}")
        public AjaxResult rejectaudit( @PathVariable("id") int id) {
          int i = erpSupplierManagementService.rejectaudit( id);
            if (i > 0) {
                return AjaxResult.success("驳回成功");
            } else {
                return AjaxResult.error("驳回失败");
            }
        }

    /*审核供应商管理 审核提交*/
    @PostMapping("/submit/{id}")
    public AjaxResult submit( @PathVariable("id") int id) {
        Map<String, Integer>  i = erpSupplierManagementService. submit( id);
        if (i.get("提交成功？") > 0) {
            return AjaxResult.success(i);
        } else {
            return AjaxResult.error("提交失败");

        }
    }

//    /*插叙你已审核的供应商*/
//    @PreAuthorize("@ss.hasPermi('purchase:purchase:query')")
//    @GetMapping(value = "/shenhesupper")
//    public AjaxResult getsupper() {
//
//        return success(erpSupplierManagementService.getsupper());
//    }


}







